<?php

namespace app\admin\controller\goods;

use service\JsonService;
use app\common\model as M;

/**
 * 商品模块
 */
class Goods extends \app\admin\controller\AuthController
{
	public $model = '\app\common\model\Goods';

    use \traits\ShotcutControllerTrait;

    /**
     * SKU列表
     * @Author   FW
     * @DateTime 2024-02-29
     * @Email    123763712@qq.com
     * @param    int              $goods_id [description]
     * @return   [type]                     [description]
     */
    public function sku_list ( int $goods_id )
    {
        $list = M\GoodsSku::field('id,sku_name')->where('goods_id',$goods_id)->select();
        return JsonService::successful($list);
    }

    /**
     * SKU保存
     * @Author   FW
     * @DateTime 2024-02-29
     * @Email    123763712@qq.com
     * @param    int              $id [description]
     * @return   [type]               [description]
     */
    public function sku_save ()
    {
        $post = $this->request->post();
        if ( !isset($post['goods_id']) ) return JsonService::fail('缺少商品ID');
        if ( !isset($post['sku_name']) ) return JsonService::fail('缺少sku_name');

        $res = M\GoodsSku::create([
            'goods_id' => $post['goods_id'],
            'sku_name' => $post['sku_name'],
            'avaiable_start' => date('Y-m-01'),
            'avaiable_end'   => date('Y-m-01' , strtotime('+3 months')),
        ]);
        if ( $res === false ) return JsonService::fail('添加失败');
        return JsonService::successful('操作成功!');
    }
}